﻿function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}
var file_type = "";
function openmedia(evt, medianame) {
    var i, x, tablinks;
    x = document.getElementsByClassName("media-content");
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("media-link");
    for (i = 0; i < x.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" w3-bottombar", " deactive");
    }
    document.getElementById(medianame).style.display = "block";
    evt.currentTarget.className = evt.currentTarget.className.replace(" deactive", " w3-bottombar");
}
function readURL(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            
            var split = e.target.result.toString().split("/");
            file_type = split[0];
            if (split[0] == "data:image") {
                document.getElementById("audio_upload").style.display = "none";
                document.getElementById("img_upload").style.display = "inline-block";
                document.getElementById("upload_name").style.display = "block";
                $('#img_upload').attr('src', e.target.result);
            }
            else {
                if (split[0] == "data:audio") {
                    document.getElementById("audio_upload").style.display = "inline-block";
                    document.getElementById("img_upload").style.display = "none";
                    $('#audio_upload').attr('src', e.target.result);
                    document.getElementById("upload_name").style.display = "block";
                }
                else {
                    document.getElementById("audio_upload").style.display = "none";
                    document.getElementById("img_upload").style.display = "none";
                    document.getElementById("upload_name").style.display = "none";
                }
            }
        }

        reader.readAsDataURL(input.files[0]);
    }
}
$(document).ready(function () {
    $("#fileUpload").change(function () {
        readURL(this);
    });
    $(".media-item").click(function () {
        var link = this.getElementsByClassName("media-link")[0].innerHTML;
        document.getElementById("library_link").value = link;
    });
    $("#btn_upload_file").click(function () {
        var file = document.getElementById("fileUpload").value;
        var name = document.getElementById("txt_file_name").value;
        if (file != "") {
            if (file_type != "data:image" && file_type != "data:audio") {
                alert("Tập tin này không thuộc hình ảnh hoặc âm thanh");
            }
            else {
                if (name != "") {
                    var file_data = document.getElementById('fileUpload').files[0];
                    if (file_data == null) return; // chưa chọn file
                    var form_data = new FormData();
                    form_data.append("fileUpload", file_data);
                    $.ajax({
                        url: '../ajax/asp/certificate/upload.aspx?type='+file_type+"&name="+name.trim(),
                        data: form_data,
                        processData: false,
                        contentType: false,
                        type: 'POST',
                        success: function (data) {
                            //$("#divTreeView").html(data);
                        }
                    });
                    document.getElementById("fileUpload").value = "";
                    document.getElementById("txt_file_name").value = "";
                    document.getElementById("upload_name").style.display = "none";
                    document.getElementById("audio_upload").style.display = "none";
                    document.getElementById("img_upload").style.display = "none";
                    document.getElementById("upload-media-modal").style.display = "none";

                    if (file_type == "data:image") {
                        document.getElementById("pic_area").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
                        
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("pic_area").innerHTML = xhttp.responseText;
                                $(".media-item").click(function () {
                                    var link = this.getElementsByClassName("media-link")[0].innerHTML;
                                    document.getElementById("library_link").value = link;
                                });
                                $(document).ready(function () {
                                    $('[data-toggle="tooltip"]').tooltip();
                                });
                                var max = document.getElementById("load_more_img").getElementsByClassName("total_media")[0].innerHTML;
                                max++; document.getElementById("load_more_img").getElementsByClassName("total_media")[0].innerHTML = max;
                                max = document.getElementById("load_more_img").getElementsByClassName("total_media")[0].innerHTML;
                                var count = $("#pic_area .media-item").length;
                                if(count < max)
                                    document.getElementById("btn_load_more_img").style.display = "inline-block";
                            }
                        };
                        sleep(5000);
                        xhttp.open("GET", "../ajax/asp/certificate/load_img.aspx", true);
                        xhttp.send();
                    }
                    else {
                        document.getElementById("audio_area").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("audio_area").innerHTML = xhttp.responseText;
                                $(".media-item").click(function () {
                                    var link = this.getElementsByClassName("media-link")[0].innerHTML;
                                    document.getElementById("library_link").value = link;
                                });
                                $(document).ready(function () {
                                    $('[data-toggle="tooltip"]').tooltip();
                                });
                                var max = document.getElementById("load_more_audio").getElementsByClassName("total_media")[0].innerHTML;
                                max++; document.getElementById("load_more_img").getElementsByClassName("total_media")[0].innerHTML = max;
                                max = document.getElementById("load_more_img").getElementsByClassName("total_media")[0].innerHTML;
                                var count = $("#audio_area .media-item").length;
                                if (count < max)
                                    document.getElementById("btn_load_more_audio").style.display = "inline-block";
                            }
                        };
                        sleep(5000);
                        xhttp.open("GET", "../ajax/asp/certificate/load_audio.aspx", true);
                        xhttp.send();
                    }
                }
                else {
                    alert("Bạn chưa đặt tên cho tập tin này");
                }
            }
        }
        else {
            alert("Không có file để Upload");
        }
    });
    $("#btn_upload_video").click(function () {
        var link = document.getElementById("txt_video_link").value;
        var name = document.getElementById("txt_video_name").value;

        if (link == "") {
            alert("Bạn chưa nhập đường dẫn cho video");
        }
        else {
            if (name == "") {
                alert("Bạn chưa đặt tên cho video");
            }
            else {
                document.getElementById("txt_video_link").value = "";
                document.getElementById("txt_video_name").value = "";
                document.getElementById("post-video-modal").style.display = "none";
                document.getElementById("video_area").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                        document.getElementById("video_area").innerHTML = xhttp.responseText;
                        $(".media-item").click(function () {
                            var link = this.getElementsByClassName("media-link")[0].innerHTML;
                            document.getElementById("library_link").value = link;

                        });
                        $(document).ready(function () {
                            $('[data-toggle="tooltip"]').tooltip();
                        });
                        var max = document.getElementById("load_more_video").getElementsByClassName("total_media")[0].innerHTML;
                        max++; document.getElementById("load_more_img").getElementsByClassName("total_media")[0].innerHTML = max;
                        max = document.getElementById("load_more_img").getElementsByClassName("total_media")[0].innerHTML;
                        var count = $("#video_area .media-item").length;
                        if (count < max)
                            document.getElementById("btn_load_more_video").style.display = "inline-block";
                    }
                };
                xhttp.open("GET", "../ajax/asp/certificate/load_video.aspx?name="+name+"&link="+link, true);
                xhttp.send();
            }
        }

    });
    $("#btn_load_more_img").click(function () {
        var count = $("#pic_area .media-item").length;
        var max = this.parentElement.getElementsByClassName("total_media")[0].innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("pic_area").innerHTML += xhttp.responseText;
                $(".media-item").click(function () {
                    var link = this.getElementsByClassName("media-link")[0].innerHTML;
                    document.getElementById("library_link").value = link;
                });
                $(document).ready(function () {
                    $('[data-toggle="tooltip"]').tooltip();
                });
                var count_later = $("#pic_area .media-item").length;
                if (max == count_later)
                    document.getElementById("btn_load_more_img").style.display = "none";
            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/load_more_img.aspx?count="+count, true);
        xhttp.send();
    });

    $("#btn_load_more_audio").click(function () {
        var count = $("#audio_area .media-item").length;
        var max = this.parentElement.getElementsByClassName("total_media")[0].innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("audio_area").innerHTML += xhttp.responseText;
                $(".media-item").click(function () {
                    var link = this.getElementsByClassName("media-link")[0].innerHTML;
                    document.getElementById("library_link").value = link;
                });
                $(document).ready(function () {
                    $('[data-toggle="tooltip"]').tooltip();
                });
                var count_later = $("#audio_area .media-item").length;
                if (max == count_later)
                    document.getElementById("btn_load_more_audio").style.display = "none";
            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/load_more_audio.aspx?count=" + count, true);
        xhttp.send();
    });

    $("#btn_load_more_video").click(function () {
        var count = $("#video_area .media-item").length;
        var max = this.parentElement.getElementsByClassName("total_media")[0].innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("video_area").innerHTML += xhttp.responseText;
                $(".media-item").click(function () {
                    var link = this.getElementsByClassName("media-link")[0].innerHTML;
                    document.getElementById("library_link").value = link;
                });
                $(document).ready(function () {
                    $('[data-toggle="tooltip"]').tooltip();
                });
                var count_later = $("#video_area .media-item").length;
                if (max == count_later)
                    document.getElementById("btn_load_more_video").style.display = "none";
            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/load_more_video.aspx?count=" + count, true);
        xhttp.send();
    });
    $("#ddl_certificate").on("change", function () {
        var cer = document.getElementById("ddl_certificate").value;
        if (cer == 0) {
            document.getElementById("test_area").style.display = "none";
            document.getElementById("edit_cer").style.display = "none";
            document.getElementById("question_area").style.display = 'none';
            document.getElementById("themcauhoi").style.display = 'none';
        }
        else {
            document.getElementById("question_area").style.display = 'none';
            document.getElementById("themcauhoi").style.display = 'none';
            document.getElementById("test_area").style.display = "block";
            document.getElementById("edit_cer").style.display = "inline-block";
            var xhttp;
            document.getElementById("test_list").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById("test_list").innerHTML = xhttp.responseText;
                    $(".test").on("click", function () {
                        document.getElementById("question_area").style.display = "block";
                        document.getElementById("themcauhoi").style.display = "none";
                        document.getElementById("question_list").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
                        var id = this.getElementsByClassName("test_id")[0].innerHTML;
                        var testlinks = document.getElementsByClassName("testlinks");
                        for (var j = 0; j < testlinks.length; j++) {
                            testlinks[j].className = testlinks[j].className.replace(" w3-leftbar w3-border-teal", "");
                        }
                        $(this).addClass(" w3-leftbar w3-border-teal");
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("question_list").innerHTML = xhttp.responseText;
                                $(".question").on("click", function () {
                                    var thutu = this.getElementsByClassName("thutu")[0].innerHTML;
                                    var id = this.getElementsByClassName("ques_id")[0].innerHTML;

                                    document.getElementById("themcauhoi").style.display = "block";
                                    var xhttp;
                                    xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function () {
                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                            document.getElementById("themcauhoi").innerHTML = xhttp.responseText;

                                            //tiny-mce
                                            tinymce.remove('textarea');
                                            tinymce.init({
                                                selector: 'textarea',
                                                height: 200,
                                                width: '75%',
                                                plugins: [
                                                  'advlist autolink lists link image charmap print preview anchor',
                                                  'searchreplace visualblocks code fullscreen furigana ',
                                                  'insertdatetime media table contextmenu paste code'
                                                ],
                                                fontsize_formats: "8px 10px 12px 14px 15px 16px 18px 20px 21px 22px",
                                                toolbar: 'fontsizeselect insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image furigana'

                                            });

                                            $(".question_edit").on("click", function () {
                                                $("#edit_ques_name").val(tinymce.activeEditor.getContent());
                                                var name = $('#edit_ques_name').val();
                                                var id = document.getElementById('edit_ques_id').innerHTML;
                                                var pa1 = document.getElementsByClassName('question_detail')[0].value;
                                                var pa2 = document.getElementsByClassName('question_detail')[1].value;
                                                var pa3 = document.getElementsByClassName('question_detail')[2].value;
                                                var pa4 = document.getElementsByClassName('question_detail')[3].value;
                                                var thutu = document.getElementById("edit_ques_thutu").innerHTML;
                                                var file = "";
                                                var pa1_id = document.getElementById("pa1").value;
                                                var pa2_id = document.getElementById("pa2").value;
                                                var pa3_id = document.getElementById("pa3").value;
                                                var pa4_id = document.getElementById("pa4").value;
                                                var type = document.getElementById("test_detail_type").value;
                                                var confirm = "";
                                                if ($('#pa1').is(':checked'))
                                                    confirm = document.getElementById("pa1").value;
                                                if ($('#pa2').is(':checked'))
                                                    confirm = document.getElementById("pa2").value;
                                                if ($('#pa3').is(':checked'))
                                                    confirm = document.getElementById("pa3").value;
                                                if ($('#pa4').is(':checked'))
                                                    confirm = document.getElementById("pa4").value;
                                                if (name == "") {
                                                    alert("Nội dung không được để trống!");
                                                }
                                                else {
                                                    if (pa1 == "")
                                                        alert("Phương án 1 trống!");
                                                    else {
                                                        if (pa2 == "")
                                                            alert("Phương án 2 trống!");
                                                        else {
                                                            if (pa3 == "")
                                                                alert("Phương án 3 trống");
                                                            else {
                                                                if (pa4 == "")
                                                                    alert("Phương án 4 trống");
                                                                else {
                                                                    if (file != "") {
                                                                        var file_data = document.getElementById('ques_file').files[0];
                                                                        if (file_data == null) return; // chưa chọn file
                                                                        var form_data = new FormData();
                                                                        form_data.append("ques_file", file_data);
                                                                        $.ajax({
                                                                            url: '../ajax/asp/certificate/upload.aspx',
                                                                            data: form_data,
                                                                            processData: false,
                                                                            contentType: false,
                                                                            type: 'POST',
                                                                            success: function (data) {
                                                                                //$("#divTreeView").html(data);
                                                                            }
                                                                        });
                                                                    }
                                                                    $.post("../ajax/asp/certificate/question_edited.aspx", { 'id': id, 'thutu': thutu, 'name': name, 'pa1': pa1, 'pa1_id': pa1_id, 'pa2': pa2, 'pa2_id': pa2_id, 'pa3': pa3, 'pa3_id': pa3_id, 'pa4': pa4, 'pa4_id': pa4_id, 'file': file, 'confirm': confirm, 'type': type });
                                                                    sleep(1000);
                                                                    var xhttp;
                                                                    xhttp = new XMLHttpRequest();
                                                                    xhttp.onreadystatechange = function () {
                                                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                            document.getElementById("themcauhoi").style.display = "none";
                                                                            document.getElementById(id).innerHTML = xhttp.responseText;
                                                                            //tiny-mce
                                                                            tinymce.remove('textarea');
                                                                            tinymce.init({
                                                                                selector: 'textarea#edit_ques_name',
                                                                                height: 200,
                                                                                width: '75%',
                                                                                plugins: [
                                                                                  'advlist autolink lists link image charmap print preview anchor',
                                                                                  'searchreplace visualblocks code fullscreen furigana ',
                                                                                  'insertdatetime media table contextmenu paste code'
                                                                                ],
                                                                                fontsize_formats: "8px 10px 12px 14px 15px 16px 18px 20px 21px 22px",
                                                                                toolbar: 'fontsizeselect insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image furigana',
                                                                                content_css: [
                                                                                  '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
                                                                                  '//www.tinymce.com/css/codepen.min.css'
                                                                                ]
                                                                            });

                                                                            $(".del_modal").click(function () {
                                                                                document.getElementById('xoacauhoi').style.display = 'block';
                                                                                document.getElementById('themcauhoi').style.display = 'none';
                                                                                $(".question").removeClass("selected");
                                                                                $(this).parent().addClass("selected");
                                                                            });
                                                                        }
                                                                    };
                                                                    xhttp.open("GET", "../ajax/asp/certificate/question_edited_show.aspx?id=" + id + "&thutu=" + thutu, true);
                                                                    xhttp.send();
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                            });
                                            $(".btn_edit_type").on("click", function () {
                                                var id = document.getElementById("test_detail_type").value;
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        document.getElementById("chinhsualoaich").innerHTML = xhttp.responseText;
                                                        document.getElementById("chinhsualoaich").style.display = 'block';
                                                        $(".btn_edited_type").on("click", function () {
                                                            var id = document.getElementById("test_type_id").innerHTML;
                                                            var type = document.getElementById("txt_test_type").value;
                                                            var xhttp;
                                                            xhttp = new XMLHttpRequest();
                                                            xhttp.onreadystatechange = function () {
                                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                    location.reload(true);
                                                                }
                                                            };
                                                            xhttp.open("GET", "../ajax/asp/test/edited_test_type.aspx?id=" + id + "&type=" + type, true);
                                                            xhttp.send();
                                                        });
                                                        $(".btn_del_type").on("click", function () {
                                                            var id = document.getElementById("test_type_id").innerHTML;
                                                            var xhttp;
                                                            xhttp = new XMLHttpRequest();
                                                            xhttp.onreadystatechange = function () {
                                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                    document.getElementById("xoaloaich").innerHTML = xhttp.responseText;
                                                                    document.getElementById("xoaloaich").style.display = 'block';
                                                                    document.getElementById("test_type_id").innerHTML = id;
                                                                    $(".btn_deleted_type").on("click", function () {
                                                                        var id = document.getElementById("test_type_id").innerHTML;
                                                                        var xhttp;
                                                                        xhttp = new XMLHttpRequest();
                                                                        xhttp.onreadystatechange = function () {
                                                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                                location.reload(true);
                                                                            }
                                                                        };
                                                                        xhttp.open("GET", "../ajax/asp/test/deleted_test_type.aspx?id=" + id, true);
                                                                        xhttp.send();
                                                                    });
                                                                }
                                                            };
                                                            xhttp.open("GET", "../ajax/asp/test/del_test_type.aspx?id=" + id, true);
                                                            xhttp.send();
                                                        });

                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/test/edit_test_type.aspx?id=" + id, true);
                                                xhttp.send();
                                            });
                                            $(".btn_add_type").on("click", function () {
                                                var type = document.getElementById("txt_add_type").value;
                                                if (type == "") {
                                                    alert("Loại câu hỏi không được để trống")
                                                }
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        location.reload(true);
                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/test/add_test_type.aspx?type=" + type, true);
                                                xhttp.send();
                                            });
                                        }
                                    };
                                    xhttp.open("GET", "../ajax/asp/certificate/question_edit.aspx?id=" + id + "&thutu=" + thutu, true);
                                    xhttp.send();
                                });
                                $(".del_modal").click(function () {
                                    document.getElementById('xoacauhoi').style.display = 'block';
                                    document.getElementById('themcauhoi').style.display = 'none';
                                    $(".question").removeClass("selected");
                                    $(this).parent().addClass("selected");
                                });
                            }
                        };

                        xhttp.open("GET", "../ajax/asp/certificate/get_test_detail.aspx?id=" + id, true);
                        xhttp.send();
                    });
                    $(".test_edit").click(function () {

                        var test_id = document.getElementById('get_id').innerHTML;
                        document.getElementById('suabai').style.display = "block";
                        document.getElementById("test_edit").innerHTML = '<div id="loading" class="w3-teal"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("test_edit").innerHTML = xhttp.responseText;
                                $(".edit-on").click(function () {
                                    if ($(this).hasClass("off")) {
                                        $(this).removeClass("off");
                                        $(this).removeClass("fa-rotate-180");
                                        $(this).removeClass("w3-text-red");
                                        $(this).addClass("w3-text-white");
                                        document.getElementById("edit_active").innerHTML = '1';
                                    }
                                    else {
                                        $(this).addClass("w3-text-red");
                                        $(this).removeClass("w3-text-white");
                                        $(this).addClass("fa-rotate-180");
                                        $(this).addClass("off");
                                        document.getElementById("edit_active").innerHTML = '0';
                                    }

                                });
                                $(".edited").on("click", function () {
                                    var id = document.getElementById("get_id").innerHTML;
                                    var name = document.getElementById("edit_name").value;
                                    var time = document.getElementById("edit_time").value;
                                    var cer = document.getElementById("ddl_edit_certificate").value;
                                    var check = "";
                                    if ($('#thuong-edit').is(':checked'))
                                        check = "0";
                                    else
                                        check = "1";
                                    var start = document.getElementById("edit_start").value;
                                    var active = document.getElementById("edit_active").innerHTML;
                                    var level = "";
                                    for (var i = 0; i < document.getElementsByClassName('edit_user_level').length; i++) {
                                        if (document.getElementsByClassName('edit_user_level')[i].checked == true) {
                                            level += document.getElementsByClassName('edit_user_level')[i].value;
                                            level += ",";
                                        }
                                    }

                                    if (name == "") {
                                        alert("Tiêu đề không được để trống!");
                                    }
                                    else {
                                        if (time == "")
                                            alert("Thời gian làm bài không được để trống!");
                                        else {
                                            if ($('#thikt-edit').is(':checked') && start == "") {
                                                alert("Ngày mở bài không được để trống hoặc sai định dạng!");
                                            }
                                            else {
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        document.getElementById("suabai").style.display = "none";
                                                        document.getElementById("test" + id).innerHTML = xhttp.responseText;
                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/certificate/test_edited.aspx?id=" + id + "&name=" + name + "&cer=" + cer + "&time=" + time + "&check=" + check + "&start=" + start + "&level=" + level + "&active=" + active, true);
                                                xhttp.send();
                                                //alert(id+ " " +name + " " + time + " " + check + " " + active + " " + start + " " + level + " " + cer);
                                            }
                                        }
                                    }
                                });
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/certificate/test_edit.aspx?id=" + test_id, true);
                        xhttp.send();
                    });
                    $("#edit_cer").on("click", function () {
                        var id = document.getElementById("ddl_certificate").value;

                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("chinhsuabang").innerHTML = xhttp.responseText;

                                document.getElementById("chinhsuabang").style.display = 'block';
                                $(".btn_edit_cer").on("click", function () {
                                    var id = document.getElementById("edit_cer_id").innerHTML;
                                    var cer = document.getElementById("txt_edit_cer").value;
                                    if (cer == "") {
                                        alert("Tên quyền không được để trống!");
                                    }
                                    else {
                                        var xhttp;
                                        xhttp = new XMLHttpRequest();
                                        xhttp.onreadystatechange = function () {
                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                location.reload(true);
                                            }
                                        };
                                        xhttp.open("GET", "../ajax/asp/certificate/edited_cer.aspx?cer=" + cer + "&id=" + id, true);
                                        xhttp.send();
                                    }
                                });
                                $(".btn_del_cer").on("click", function () {
                                    var id = document.getElementById("edit_cer_id").innerHTML;
                                    var xhttp;
                                    xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function () {
                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                            document.getElementById("xoabang").innerHTML = xhttp.responseText;
                                            document.getElementById("xoabang").style.display = 'block';
                                            $(".btn_deleted_cer").on("click", function () {
                                                var id = document.getElementById("edit_cer_id").innerHTML;
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        location.reload(true);

                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/certificate/deleted_cer.aspx?id=" + id, true);
                                                xhttp.send();
                                            });
                                        }
                                    };
                                    xhttp.open("GET", "../ajax/asp/certificate/del_cer.aspx?id=" + id, true);
                                    xhttp.send();
                                });
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/certificate/edit_cer.aspx?id=" + id, true);
                        xhttp.send();
                    });

                }
            };
            xhttp.open("GET", "../ajax/asp/certificate/get_test.aspx?cer=" + cer, true);
            xhttp.send();
        }
    });

    $(".btn_add_test").click(function () {
        var title = document.getElementById("new_test_name").value;
        var time = document.getElementById("new_test_time").value;
        var check = "";
        if ($('#thuong').is(':checked'))
            check = "0";
        else
            check = "1";
        var start = document.getElementById("new_test_start").value;
        var level = "";
        for (var i = 0; i < document.getElementsByClassName('user_level').length; i++) {
            if (document.getElementsByClassName('user_level')[i].checked == true) {
                level += document.getElementsByClassName('user_level')[i].value;
                level += ",";
            }
        }
        var active = document.getElementById("active").innerHTML;
        var cer = document.getElementById("ddl_certificate").value;
        if (title == "")
            alert("Tiêu đề không được để trống!");
        else {
            if (time == "")
                alert("Thời gian làm bài không được để trống!");
            else {
                if ($('#thikt').is(':checked') && start == "") {
                    alert("Ngày mở bài không được để trống hoặc sai định dạng");
                }
                else {
                    //alert(title + cer + time + check + start + level + active);
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            document.getElementById("thembai").style.display = "none";
                            document.getElementById("test_list").innerHTML += xhttp.responseText;
                            $(".test").on("click", function () {
                                document.getElementById("themcauhoi").style.display = "none";
                                document.getElementById("question_area").style.display = "block";
                                document.getElementById("question_list").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
                                var id = this.getElementsByClassName("test_id")[0].innerHTML;
                                var xhttp;
                                xhttp = new XMLHttpRequest();
                                xhttp.onreadystatechange = function () {
                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                        document.getElementById("question_list").innerHTML = xhttp.responseText;
                                        $(".question").on("click", function () {
                                            var thutu = this.getElementsByClassName("thutu")[0].innerHTML;
                                            var id = this.getElementsByClassName("ques_id")[0].innerHTML;
                                            document.getElementById("themcauhoi").style.display = "block";
                                            var xhttp;
                                            xhttp = new XMLHttpRequest();
                                            xhttp.onreadystatechange = function () {
                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                    document.getElementById("themcauhoi").innerHTML = xhttp.responseText;

                                                    //tiny-mce
                                                    tinymce.remove('textarea');
                                                    tinymce.init({
                                                        selector: 'textarea#add_ques_name',
                                                        height: 200,
                                                        width: '75%',
                                                        plugins: [
                                                          'advlist autolink lists link image charmap print preview anchor',
                                                          'searchreplace visualblocks code fullscreen furigana ',
                                                          'insertdatetime media table contextmenu paste code'
                                                        ],
                                                        fontsize_formats: "8px 10px 12px 14px 15px 16px 18px 20px 21px 22px",
                                                        toolbar: 'fontsizeselect insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image furigana',
                                                        content_css: [
                                                          '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
                                                          '//www.tinymce.com/css/codepen.min.css'
                                                        ]
                                                    });

                                                    $(".question_edit").on("click", function () {
                                                        $("#edit_ques_name").val(tinymce.activeEditor.getContent());
                                                        var name = $('#edit_ques_name').val();
                                                        var id = document.getElementById('edit_ques_id').innerHTML;
                                                        var pa1 = document.getElementsByClassName('question_detail')[0].value;
                                                        var pa2 = document.getElementsByClassName('question_detail')[1].value;
                                                        var pa3 = document.getElementsByClassName('question_detail')[2].value;
                                                        var pa4 = document.getElementsByClassName('question_detail')[3].value;
                                                        var thutu = document.getElementById("edit_ques_thutu").innerHTML;
                                                        var file = "";
                                                        var pa1_id = document.getElementById("pa1").value;
                                                        var pa2_id = document.getElementById("pa2").value;
                                                        var pa3_id = document.getElementById("pa3").value;
                                                        var pa4_id = document.getElementById("pa4").value;
                                                        var type = document.getElementById("test_detail_type").value;
                                                        var confirm = "";
                                                        if ($('#pa1').is(':checked'))
                                                            confirm = document.getElementById("pa1").value;
                                                        if ($('#pa2').is(':checked'))
                                                            confirm = document.getElementById("pa2").value;
                                                        if ($('#pa3').is(':checked'))
                                                            confirm = document.getElementById("pa3").value;
                                                        if ($('#pa4').is(':checked'))
                                                            confirm = document.getElementById("pa4").value;
                                                        if (name == "") {
                                                            alert("Nội dung không được để trống!");
                                                        }
                                                        else {
                                                            if (pa1 == "")
                                                                alert("Phương án 1 trống!");
                                                            else {
                                                                if (pa2 == "")
                                                                    alert("Phương án 2 trống!");
                                                                else {
                                                                    if (pa3 == "")
                                                                        alert("Phương án 3 trống");
                                                                    else {
                                                                        if (pa4 == "")
                                                                            alert("Phương án 4 trống");
                                                                        else {
                                                                            if (file != "") {
                                                                                var file_data = document.getElementById('ques_file').files[0];
                                                                                if (file_data == null) return; // chưa chọn file
                                                                                var form_data = new FormData();
                                                                                form_data.append("ques_file", file_data);
                                                                                $.ajax({
                                                                                    url: '../ajax/asp/certificate/upload.aspx',
                                                                                    data: form_data,
                                                                                    processData: false,
                                                                                    contentType: false,
                                                                                    type: 'POST',
                                                                                    success: function (data) {
                                                                                        //$("#divTreeView").html(data);
                                                                                    }
                                                                                });
                                                                            }
                                                                            $.post("../ajax/asp/certificate/question_edited.aspx", { 'id': id, 'thutu': thutu, 'name': name, 'pa1': pa1, 'pa1_id': pa1_id, 'pa2': pa2, 'pa2_id': pa2_id, 'pa3': pa3, 'pa3_id': pa3_id, 'pa4': pa4, 'pa4_id': pa4_id, 'file': file, 'confirm': confirm, 'type': type });
                                                                            sleep(1000);
                                                                            var xhttp;
                                                                            xhttp = new XMLHttpRequest();
                                                                            xhttp.onreadystatechange = function () {
                                                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                                    document.getElementById("themcauhoi").style.display = "none";
                                                                                    document.getElementById(id).innerHTML = xhttp.responseText;
                                                                                    tiny - mce
                                                                                    tinymce.remove('textarea');
                                                                                    tinymce.init({
                                                                                        selector: 'textarea#edit_ques_name',
                                                                                        height: 200,
                                                                                        width: '75%',
                                                                                        plugins: [
                                                                                          'advlist autolink lists link image charmap print preview anchor',
                                                                                          'searchreplace visualblocks code fullscreen furigana ',
                                                                                          'insertdatetime media table contextmenu paste code'
                                                                                        ],
                                                                                        fontsize_formats: "8px 10px 12px 14px 15px 16px 18px 20px 21px 22px",
                                                                                        toolbar: 'fontsizeselect insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image furigana',
                                                                                        content_css: [
                                                                                          '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
                                                                                          '//www.tinymce.com/css/codepen.min.css'
                                                                                        ]
                                                                                    });

                                                                                    $(".del_modal").click(function () {
                                                                                        document.getElementById('xoacauhoi').style.display = 'block';
                                                                                        document.getElementById('themcauhoi').style.display = 'none';
                                                                                        $(".question").removeClass("selected");
                                                                                        $(this).parent().addClass("selected");
                                                                                    });
                                                                                }
                                                                            };
                                                                            xhttp.open("GET", "../ajax/asp/certificate/question_edited_show.aspx?id=" + id + "&thutu=" + thutu, true);
                                                                            xhttp.send();
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    });
                                                }
                                            };
                                            xhttp.open("GET", "../ajax/asp/certificate/question_edit.aspx?id=" + id + "&thutu=" + thutu, true);
                                            xhttp.send();
                                        });
                                        $(".del_modal").click(function () {
                                            document.getElementById('xoacauhoi').style.display = 'block';
                                            document.getElementById('themcauhoi').style.display = 'none';
                                            $(".question").removeClass("selected");
                                            $(this).parent().addClass("selected");
                                        });
                                    }
                                };

                                xhttp.open("GET", "../ajax/asp/certificate/get_test_detail.aspx?id=" + id, true);
                                xhttp.send();
                            });
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/certificate/add_test.aspx?title=" + title + "&cer=" + cer + "&time=" + time + "&check=" + check + "&start=" + start + "&level=" + level + "&active=" + active, true);
                    xhttp.send();
                }
            }
        }

    });

    $(".btn_add_cer").on("click", function () {
        var cer = document.getElementById("txt_add_cer").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/add_cer.aspx?cer=" + cer, true);
        xhttp.send();
    });


});